package com.kaoshi.myweb.webstruts.support;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.ServletDispatcherResult;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.interceptor.Interceptor;

/*
 * 辅助interceptor
 * 输出 Action、Method、param、view路径 信息
 */
public class MyLoggerInterceptor implements Interceptor {

	@Override
	public void destroy() {
	}

	@Override
	public void init() {
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		System.out.println("logger begin");
		
		ActionContext context = invocation.getInvocationContext();
		HttpServletRequest request= (HttpServletRequest) context.get(StrutsStatics.HTTP_REQUEST);
		System.out.println("URL:" + request.getRequestURI());
		System.out.println("Action:" + invocation.getAction().getClass().getName());
		System.out.println("Method:" + invocation.getProxy().getMethod());
		
		Map<String, Object> params = invocation.getInvocationContext().getParameters();
		
		for (String key : params.keySet()) {
			Object object = params.get(key);
			
			if(object instanceof String[])
			{
				String[] arrStrings = (String[]) object;
				System.out.println("param:" + key);
				
				for (String value : arrStrings) {
					System.out.println(value);
				}
			}
		}
		
		String resultCode = invocation.invoke();
		
		System.out.println(resultCode);
		Result result = invocation.getResult();
		if(result instanceof ServletDispatcherResult)
		{
			ServletDispatcherResult dispatcherResult = (ServletDispatcherResult) result;
			System.out.println("JSP:" + dispatcherResult.getLastFinalLocation());;
		}
		
		System.out.println("logger end");
		return resultCode;
	}

}
